/// ZVT协议枚举类型

/// 命令类型枚举
enum ZVTCommandType {
  AUTHORIZATION(0x50, "授权"),
  REFUND(0x51, "退款"),
  END_OF_DAY(0x70, "日终"),
  ABORT(0x60, "中止"),
  REVERSAL(0x40, "冲正"),
  REPEAT_RECEIPT(0x64, "重复打印收据");

  const ZVTCommandType(this.code, this.description);
  final int code;
  final String description;

  /// 获取命令代码
  int get commandCode => code;

  /// 获取命令描述
  String get commandDescription => description;

  /// 根据代码获取枚举
  static ZVTCommandType? fromCode(int code) {
    for (ZVTCommandType type in ZVTCommandType.values) {
      if (type.code == code) return type;
    }
    return null;
  }
}

/// 响应类型枚举
enum ZVTResponseType {
  ACK(0x80, "确认"),
  STATUS(0x04, "状态信息"),
  debugPrint(0x06, "打印数据"),
  SUCCESS(0x0F, "成功"),
  ERROR(0x1E, "错误");

  const ZVTResponseType(this.code, this.description);
  final int code;
  final String description;

  /// 获取响应代码
  int get responseCode => code;

  /// 获取响应描述
  String get responseDescription => description;

  /// 根据代码获取枚举
  static ZVTResponseType? fromCode(int code) {
    for (ZVTResponseType type in ZVTResponseType.values) {
      if (type.code == code) return type;
    }
    return null;
  }
}

/// 状态码枚举
/// 按ZVT官方文档标准，覆盖所有常见状态码
enum ZVTStatusCode {
  WAIT(0x0E, "请稍等..."),
  AUTO_END_OF_DAY(0x0F, "PT正在开始自动日终批次"),
  INVALID_CARD(0x10, "无效卡"),
  BALANCE_DISPLAY(0x11, "余额显示"),
  SYSTEM_FAULT(0x12, "系统故障"),
  PAYMENT_IMPOSSIBLE(0x13, "支付不可能"),
  INSUFFICIENT_BALANCE(0x14, "余额不足"),
  PIN_ERROR(0x15, "PIN错误"),
  INSUFFICIENT_LIMIT(0x16, "限额不足"),
  WAIT_AGAIN(0x17, "请稍等..."),
  PIN_ATTEMPTS_EXCEEDED(0x18, "PIN尝试次数超限"),
  CARD_DATA_ERROR(0x19, "卡片数据错误"),
  SERVICE_MODE(0x1A, "服务模式"),
  APPROVED_REFILL(0x1B, "批准，请加满"),
  APPROVED_TAKE_GOODS(0x1C, "批准，请取走商品"),
  REJECTED(0x1D, "拒绝"),
  WAIT_PHONE(0x26, "PT等待输入手机号码"),
  WAIT_PHONE_REPEAT(0x27, "PT等待重复手机号码"),
  CURRENCY_SELECTION(0x28, "货币选择，请稍等..."),
  LANGUAGE_SELECTION(0x29, "语言选择，请稍等..."),
  INSERT_CARD_RECHARGE(0x2A, "请插入卡片进行充值"),
  EMERGENCY_TRANSACTION(0x2B, "紧急交易，请稍等"),
  APPLICATION_SELECTION(0x2C, "应用选择，请稍等"),
  WATCH_PIN_PAD_REMOVE_CARD(0x41, "请观看PIN-Pad，请取出卡片！"),
  WATCH_PIN_PAD_REMOVE_CARD_2(0x42, "请观看PIN-Pad，请取出卡片！"),
  NOT_ACCEPTED_REMOVE_CARD(0x43, "不接受，请取出卡片！"),
  WAIT_FEP_RESPONSE(0x44, "PT等待FEP响应，请取出卡片！"),
  AUTO_REVERSAL(0x45, "PT正在发送自动冲正，请取出卡片！"),
  POST_AUTHORIZATION(0x46, "PT正在发送后记账，请取出卡片！"),
  CARD_NOT_ALLOWED(0x47, "卡片不被允许，请取出卡片！"),
  CARD_UNKNOWN(0x48, "卡片未知/未定义，请取出卡片！"),
  CARD_EXPIRED(0x49, "卡片过期，请取出卡片！"),
  REMOVE_CARD(0x4B, "请取出卡片！"),
  CARD_UNREADABLE(0x4C, "卡片不可读，请取出卡片！"),
  PROCESSING_ERROR(0x4D, "处理错误，请取出卡片！"),
  WAIT_REMOVE_CARD(0x4E, "请稍等，请取出卡片！"),
  AUTO_END_OF_DAY_REMOVE_CARD(0x4F, "PT正在开始自动日终批次，请取出卡片！"),
  INVALID_CARD_REMOVE(0x50, "无效卡，请取出卡片！"),
  BALANCE_DISPLAY_REMOVE(0x51, "余额显示，请取出卡片！"),
  SYSTEM_FAULT_REMOVE(0x52, "系统故障，请取出卡片！"),
  PAYMENT_IMPOSSIBLE_REMOVE(0x53, "支付不可能，请取出卡片！"),
  INSUFFICIENT_BALANCE_REMOVE(0x54, "余额不足，请取出卡片！"),
  PIN_ERROR_REMOVE(0x55, "PIN错误，请取出卡片！"),
  INSUFFICIENT_LIMIT_REMOVE(0x56, "限额不足，请取出卡片！"),
  WAIT_REMOVE_CARD_2(0x57, "请稍等...，请取出卡片！"),
  PIN_ATTEMPTS_EXCEEDED_REMOVE(0x58, "PIN尝试次数超限，请取出卡片！"),
  CARD_DATA_ERROR_REMOVE(0x59, "卡片数据错误，请取出卡片！"),
  SERVICE_MODE_REMOVE(0x5A, "服务模式，请取出卡片！"),
  APPROVED_REFILL_REMOVE(0x5B, "批准，请加满，请取出卡片！"),
  APPROVED_TAKE_GOODS_REMOVE(0x5C, "批准，请取走商品，请取出卡片！"),
  REJECTED_REMOVE(0x5D, "拒绝，请取出卡片！"),
  CONTACTLESS_COMPLETE(0x5E, "非接触式卡片访问完成信号"),
  WAIT_PHONE_REMOVE(0x66, "PT等待输入手机号码，请取出卡片！"),
  WAIT_PHONE_REPEAT_REMOVE(0x67, "PT等待重复手机号码，请取出卡片！"),
  CUSTOMER_CARD_INSERTED(0x68, "PT检测到客户卡片插入"),
  SELECT_DCC(0x69, "请选择DCC"),
  WAIT_MILEAGE(0xC7, "PT等待输入里程"),
  WAIT_CASHIER(0xC8, "PT等待收银员"),
  AUTO_DIAGNOSTIC(0xC9, "PT正在开始自动诊断"),
  AUTO_INITIALIZATION(0xCA, "PT正在开始自动初始化"),
  MERCHANT_LOG_FULL(0xCB, "商户日志已满"),
  DEBIT_NOTIFICATION_IMPOSSIBLE(0xCC, "借记通知不可能，需要PIN"),
  CONNECTION_DIALING(0xD2, "连接拨号"),
  DIALING_CONNECTION_ESTABLISHED(0xD3, "拨号连接已建立"),
  WAIT_APPLICATION_SELECTION(0xE0, "PT等待应用选择"),
  WAIT_LANGUAGE_SELECTION(0xE1, "PT等待语言选择"),
  REQUEST_CLEAN_CARD(0xE2, "PT请求使用清洁卡"),
  OFFLINE(0xF1, "离线"),
  ONLINE(0xF2, "在线"),
  OFFLINE_TRANSACTION(0xF3, "离线交易"),
  NO_APPROPRIATE_STATUS(0xFF, "未知错误,请查看终端状态"),
  USER_CANCELLED(0x6C, "用户取消");

  const ZVTStatusCode(this.code, this.description);
  final int code;
  final String description;

  /// 获取状态码
  int get statusCode => code;

  /// 获取状态描述
  String get statusDescription => description;

  /// 根据代码获取枚举
  static ZVTStatusCode? fromCode(int code) {
    for (ZVTStatusCode status in ZVTStatusCode.values) {
      if (status.code == code) return status;
    }
    return null;
  }
}

/// 字段标识符枚举
enum ZVTFieldIdentifier {
  AMOUNT(0x4F, "金额"),
  CURRENCY(0x5A, "货币代码"),
  PAYMENT_TYPE(0x9C, "支付类型"),
  TIMEOUT(0x01, "超时时间"),
  MAX_STATUS(0x02, "最大状态信息"),
  RECEIPT_NO(0x87, "收据号"),
  RESULT_CODE(0x98, "结果代码"),
  TLV_CONTAINER(0x06, "TLV容器");

  const ZVTFieldIdentifier(this.code, this.description);
  final int code;
  final String description;

  /// 获取字段代码
  int get fieldCode => code;

  /// 获取字段描述
  String get fieldDescription => description;

  /// 根据代码获取枚举
  static ZVTFieldIdentifier? fromCode(int code) {
    for (ZVTFieldIdentifier field in ZVTFieldIdentifier.values) {
      if (field.code == code) return field;
    }
    return null;
  }
}

/// ZVT协议枚举类型（兼容性包装类）
class ZVTEnums {
  /// 命令类型枚举
  static ZVTCommandType get CommandType => ZVTCommandType.AUTHORIZATION;

  /// 响应类型枚举
  static ZVTResponseType get ResponseType => ZVTResponseType.ACK;

  /// 状态码枚举
  static ZVTStatusCode get StatusCode => ZVTStatusCode.WAIT;

  /// 字段标识符枚举
  static ZVTFieldIdentifier get FieldIdentifier => ZVTFieldIdentifier.AMOUNT;
}
